package com.luckynote.common.base.util;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * <pre>
 * *********************************************
 * Copyright LUCKY-NOTE.
 * All rights reserved.
 * ****************************************************
 *  Version       Date         Author            Desc
 *    v1.0       2019/6/29      bingo       GSON工具类
 * ****************************************************
 * </pre>
 */
public class GsonUtil {

    private static String[] excluseClasses = { "" };

    private static final ThreadLocal<Gson> LOCAL = new ThreadLocal<Gson>() {
        @Override
        protected Gson initialValue() {
            return new GsonBuilder()
                    .setExclusionStrategies(new ExclusionStrategy() {
                        @Override
                        public boolean shouldSkipField(FieldAttributes f) {
                            for (int i = 0; i < excluseClasses.length; i++) {
                                if(excluseClasses[i] != null
                                        && excluseClasses[i].equals(f.getDeclaringClass().getSimpleName())){
                                    return true;
                                }
                            }
                            return false;
                        }

                        @Override
                        public boolean shouldSkipClass(Class<?> clazz) {
                            return false;
                        }
                    }).disableHtmlEscaping().serializeNulls()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        }
    };

    public static Gson getGson() {
        return LOCAL.get();
    }
}
